import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () =>
    import ( /* webpackChunkName: "login_home_welcome" */ './../components/Login.vue')
const Home = () =>
    import ( /* webpackChunkName:"login_home_welcome" */ './../components/Home.vue')
const Welcome = () =>
    import ( /* webpackChunkName:"login_home_welcome" */ './../components/Welcome.vue')

const Users = () =>
    import ( /* webpackChunkName:"Users_Rights_roles" */ './../components/user/users.vue')
const Rights = () =>
    import ( /* webpackChunkName:"Users_Rights_roles" */ './../components/power/Rights')
const Roles = () =>
    import ( /* webpackChunkName:"Users_Rights_roles" */ './../components/power/Roles')

const Cate = () =>
    import ( /* webpackChunkName:"Cate_Params" */ './../components/goods/Cate.vue')
const Params = () =>
    import ( /* webpackChunkName:"Cate_Params" */ './../components/goods/Params.vue')

const Goods = () =>
    import ( /* webpackChunkName:"Goods_add" */ './../components/goods/List.vue')
const add = () =>
    import ( /* webpackChunkName:"Goods_add" */ './../components/goods/add.vue')

const Order = () =>
    import ( /* webpackChunkName:"Order_report" */ './../components/order/Order.vue')
const report = () =>
    import ( /* webpackChunkName:"Order_report" */ './../components/report/report.vue')



Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: '/home'
    },
    {
        path: '/login',
        component: Login
    },
    {
        path: '/home',
        component: Home,
        redirect: '/welcome',
        children: [{
                path: '/welcome',
                component: Welcome
            },
            {
                path: '/users',
                component: Users
            },
            {
                path: '/rights',
                component: Rights
            },
            {
                path: '/roles',
                component: Roles
            },
            {
                path: '/categories',
                component: Cate
            },
            {
                path: '/params',
                component: Params
            },
            {
                path: '/goods',
                component: Goods
            },
            {
                path: '/goods/add',
                component: add
            },
            {
                path: '/orders',
                component: Order
            },
            {
                path: '/reports',
                component: report
            }
        ]
    }
]

const router = new VueRouter({
    routes
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

// 挂在路由导航守卫
router.beforeEach((to, from, next) => {
    // 将要访问路径
    // 代表从那个路跳转过来的
    // next 是一个函数，表示放行
    //  next() 放行 next('/login')
    if (to.path === '/login') return next()

    //  获取token
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) {
        return next('/login')
    }
    next()
})

export default router